package fy.hr.operator.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import fy.form.listen.operate.AForm_Save_Operator;
import fy.form.listen.operate.IFormOperator;
import fy.hr.bean.AppraisalBean;
import fy.hr.dao.AppraisalDao;
import fy.msg.MessageSender;
import fy.util.MyUtil;

public class RecommendPersonFormOperator extends AForm_Save_Operator implements
		IFormOperator {

	private static Log log = LogFactory
			.getLog(RecommendPersonFormOperator.class);

	private String number;
	private String title;

	private List<String> idArray = new ArrayList<String>();
	private List<String> deptManagerOptions = new ArrayList<String>();
	private List<String> zhaoPinOptions = new ArrayList<String>();
	private List<String> auditionResultOptions = new ArrayList<String>();
	private List<String> employOptions = new ArrayList<String>();

	public String getFormName() {
		return "应聘候选人评估推荐表";
	}

	public boolean save() {
		boolean success = false;

		if (idArray.size() == deptManagerOptions.size()
				&& idArray.size() == zhaoPinOptions.size()
				&& idArray.size() == auditionResultOptions.size()
				&& idArray.size() == employOptions.size()) {
			List<AppraisalBean> beans = new ArrayList<AppraisalBean>();
			for (int i = 0, length = idArray.size(); i < length; i++) {
				AppraisalBean bean = new AppraisalBean();

				bean.setId(idArray.get(i));
				bean.setAppraisal(new StringBuilder().append("招聘负责人意见：")
						.append(zhaoPinOptions.get(i)).append("；\n用人部门意见：")
						.append(deptManagerOptions.get(i)).append("；\n面试后意见：")
						.append(auditionResultOptions.get(i))
						.append("；\n面试结果：").append(employOptions.get(i))
						.append("。").toString());

				beans.add(bean);
			}

			if (beans.size() > 0) {
				AppraisalDao dao = new AppraisalDao();
				dao.setNumber(number);
				dao.setTitle(title);
				dao.setBeans(beans);
				if (!(success = dao.save())) {
					String msg = new StringBuilder()
							.append("《应聘候选人评估推荐表》人员评价写入HR人才库失败，单据编号为“")
							.append(number).append("”，标题为“").append(title)
							.append("”，请用户手工维护。谢谢！").toString();
					MessageSender.sendMsg(super.formBean.getOperatorID(), msg);
					log.error(msg);
				}
			} else {
				String msg = new StringBuilder()
						.append("《应聘候选人评估推荐表》获取推荐人员信息为空，无法将人员评价写入HR人才库，单据编号为“")
						.append(number).append("”，标题为“").append(title)
						.append("”，请用户手工维护。谢谢！").toString();
				MessageSender.sendMsg(super.formBean.getOperatorID(), msg);
				log.error(msg);
			}
		} else {
			String msg = new StringBuilder()
					.append("《应聘候选人评估推荐表》获取推荐人员信息为空，无法将人员评价写入HR人才库，单据编号为“")
					.append(number).append("”，标题为“").append(title)
					.append("”，请用户手工维护。谢谢！").toString();
			MessageSender.sendMsg(super.formBean.getOperatorID(), msg);
			log.error(msg);
		}

		return success;
	}

	@Override
	protected void parseOtherField_FirstStep(com.seeyon.v3x.services.flow.bean.FlowExport flowExport) {
		title = flowExport.getFlowTitle();
	}

	@Override
	protected void parseValueExport(com.seeyon.v3x.services.form.bean.ValueExport ve) {
		String name = ve.getDisplayName();
		String value = ve.getValue();
		if ("单据编号".equals(name)) {
			number = value;
		} else if ("人员身份证号".equals(name)) {
			idArray.add(value);
		} else if ("招聘负责人意见".equals(name)) {
			zhaoPinOptions.add(MyUtil.replaceXMLString(value));
		} else if ("用人部门意见".equals(name)) {
			deptManagerOptions.add(MyUtil.replaceXMLString(value));
		} else if ("是否录用".equals(name)) {
			employOptions.add(value.equals("0") ? "面试未通过" : "面试通过");
		} else if ("面试意见".equals(name)) {
			auditionResultOptions.add(MyUtil.replaceXMLString(value));
		}
		name = null;
		value = null;
	}

}
